package com.xcx.minservice.controller;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commoncore.request.miniorequest.GetUserFileTypeRequest;

import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import com.xcx.minservice.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;


@Api(tags = "文件管理api")
@RestController
@RequestMapping("/minio")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin
    public class MinioController {

        final  private MinioService minioService;
        @ApiOperation(value = "我的头像/文件/视频/音频上传)", httpMethod = "POST")
        @ApiImplicitParams({
                @ApiImplicitParam(value = "上传文件(仅需要文件名）", name = "file", required = true, allowMultiple = true, dataType = "MultipartFile"),
                @ApiImplicitParam(value = "用户openid", name = "username", required = true),
                @ApiImplicitParam(value = "文件类型/1-img,2-audio,3-video,4-doc", name = "source", required = true,dataType = "Int"),
                @ApiImplicitParam(value = "文件所属1头像2我的相册3我的视频4我的音频", name = "type", required = true,dataType = "Int")
        })
        @PostMapping(value = "upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
        public ReturnMessage<Object> upload(@RequestPart("file") MultipartFile file,@RequestParam("username") String username,@RequestParam("source") Integer source,@RequestParam("type") Integer type) {

            return minioService.upload(file,username,source,type);
        }


    @ApiOperation(value = "批量上传图片(我的相册)", httpMethod = "POST")
    @PostMapping(value = "uploads", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "files", value = "文件数组(使用postman测试)", allowMultiple = true, dataType = "MultipartFile[]"),
            @ApiImplicitParam(value = "用户openid", name = "username", required = true)

    })
    public ReturnMessage<List<String>> uploads(MultipartFile[] files,String username) {

        return minioService.uploads(files,username);
    }

//    @ApiOperation(value = "文件预览")
//    @GetMapping(value = "preview")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "文件预览", name = "fileName", required = true,example = "img/d10ad9adfc3d4706861acef1d9038ad6.png")
//    })
//    public ReturnMessage<Object> preview(String fileName) {
//
//        return minioService.preview(fileName);
//    }



    @ApiOperation(value = "根据用户所属浏览文件")
    @PostMapping(value = "previewByUserName")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "文件预览", name = "username", required = true,example = "")
//    })
    public ReturnMessage<Object> previewByUserName(@RequestBody @Valid GetUserFileTypeRequest request, BindingResult br) {


        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }

        return minioService.previewByUserName(request);
    }



    @ApiOperation(value = "文件下载")
    @GetMapping("download")
    public void download(String fileName, HttpServletResponse res) {

        minioService.download(fileName,res);
    }



    @ApiOperation(value = "文件批量删除")
    @PostMapping("remove")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "文件删除", name = "fileName", required = true,example = "img/d10ad9adfc3d4706861acef1d9038ad6.png")
//    })
    public ReturnMessage<Object> remove(@RequestBody @Valid GetUserFileTypeRequest request, BindingResult br) {

        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }

        return minioService.remove(request);
    }

//    @ApiOperation(value = "查询用户所有文件信息", httpMethod = "POST")
//    @PostMapping(value = "/getUserFiles")
//    public ReturnMessage<List<FileSystem>> getUserFiles(@RequestBody GetUserFilesRequest request) {
//
//        return minioService.getUserFiles(request);
//    }


//    @ApiOperation(value = "根据用户所属查询图片/视频", httpMethod = "POST")
//    @PostMapping(value = "/getUserFilesByType")
//    public ReturnMessage<List<FileSystem>> getUserFilesByType(@RequestBody  @Valid GetUserFileTypeRequest request, BindingResult br) {
//        log.info("Request:{}", request);
//        if (request == null) {
//            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
//        }
//        if (br.hasErrors()) {
//            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
//        }
//
//        return minioService.getUserFilesByType(request);
//    }























}